﻿// Study0408.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
// 第四章 类和对象 实验
//例题1：
//声明一个CPU类，包含等级rank，频率frequency，电压voltage等属性，有两个共有成员函数run，stop
//其中
//rank为枚举类型CPU_Rank,声明enum CPU_Rank{P1=1,P2,P3,P4,P5,P6,P7}，frequency为单位是Mhz的整型数，voltage为浮点型的电压值。

#include <iostream>
using namespace std;
// CPU
enum CPU_Rank {
    P1 = 1, P2, P3, P4, P5, P6, P7
};

class CPU
{
public:
    CPU() = default;
    CPU(CPU_Rank r, int f, double v);
    CPU(const CPU& cpu);
    ~CPU();
    void SetRank(CPU_Rank r) 
    {
        rank=r;
    }
    void SetFrequency(int f)
    {
       frequency=f;
    }
    void SetVoltage(double v)
    {
        voltage=v;
    }
    CPU_Rank GetRank() const {
        return rank;
    }
    int GetFrequency() const
    {
        return frequency;
    }
    double GetVoltage() const
    {
        return voltage;
    }

    void run() {
        cout << "CPU开始运行！"<<endl;
    };
    void stop() {
        cout << "CPU结束运行！"<< endl;
    };
private:
    CPU_Rank rank;
    int frequency;
    double voltage;
};

CPU::CPU(CPU_Rank r, int f, double v):rank(r),frequency(f),voltage(v)
{
    cout << "构造了一个CPU！"<<endl;
}

CPU::CPU(const CPU& cpu)
{
    rank = cpu.GetRank();
    frequency = cpu.GetFrequency();
    voltage = cpu.GetVoltage();
    cout << "复制构造了一个CPU！"<<endl;
}

CPU::~CPU()
{
    cout << "析构了一个CPU" << endl;
}

// RAM
enum RAMType {
    DDR4=1, DDR3, DDR2
};

class RAM
{
public:
    RAM() = default;
    RAM(int cap, RAMType t, int f) :capacity(cap), type(t), frequency(f) {
        cout << "构造了一个RAM！" << endl;
    }
    RAM(const RAM& ram) {
        capacity = ram.GetCapacity();
        type = ram.GetType();
        frequency = ram.GetFrequency();
        cout << "复制构造了一个RAM！" << endl;
    }

    ~RAM() {
        cout << "析构了一个RAM！" << endl;
    }
    void SetCapacity(int cap) {
        capacity = cap;
    };
    int GetCapacity() const {
        return capacity;
    }
    void SetType(RAMType t) {
        type = t;
    };
    RAMType GetType() const {
        return type;
    }
    void SetFrequency(int f) {
        frequency = f;
    };
    int GetFrequency() const {
        return frequency;
    }
    void run() {
        cout << "RAM开始运行！" << endl;
    }
    void stop() {
        cout << "RAM结束运行！" << endl;
    }
private:
    int capacity;
    RAMType type;
    int frequency;
};

// RAM
enum InterType {
   SATA=1,USB
};
enum InstallMode {
    external = 1, built_in
};
class CD_ROM
{
public:
    CD_ROM() = default;
    CD_ROM(InterType t,int c,InstallMode m) :interface(t),cache(c),mode(m) {
        cout << "构造了一个CD_ROM！" << endl;
    }
    CD_ROM(const CD_ROM& cd_rom) {
        interface = cd_rom.interface;
        cache = cd_rom.cache;
        mode = cd_rom.mode;
        cout << "复制构造了一个CD_ROM！" << endl;
    }

    ~CD_ROM() {
        cout << "析构了一个CD_ROM！" << endl;
    }
    void SetInterface(InterType t) {
        interface = t;
    };
    int GetInterface() const {
        return interface;
    }
    void SetCache(int c) {
        cache = c;
    };
    int GetCache() const {
        return cache;
    }
    void SetMode(InstallMode m) {
        mode = m;
    };
    InstallMode GetMode() const {
        return mode;
    }
    void run() {
        cout << "CD_ROM开始运行！" << endl;
    }
    void stop() {
        cout << "CD_ROM结束运行！" << endl;
    }
private:
    InterType interface;
    int cache;
    InstallMode mode;
};

//computer
class Computer
{
public:
    Computer(CPU c, RAM r, CD_ROM rom, unsigned int s, unsigned int b);
    ~Computer();
void run(){
    my_cpu.run();
    my_ram.run();
    my_cdrom.run();
    cout << "Computer开始运行！" << endl;
}
void stop() {
    my_cpu.stop();
    my_ram.stop();
    my_cdrom.stop();
    cout << "Computer结束运行！" << endl;
}
private:
    CPU my_cpu;
    RAM my_ram;
    CD_ROM my_cdrom;
    unsigned int storage;
    unsigned int bandwith;

};

Computer::Computer(CPU c, RAM r, CD_ROM rom, unsigned s, unsigned b):my_cpu(c),my_ram(r),my_cdrom(rom),storage(s),bandwith(b)
{
    cout << "已生成构造一个Computer！" << endl;
}

Computer::~Computer()
{
    cout << "析构了一个Computer" << endl;
}


int main()
{
    CPU a(P6, 300, 2.8);
    a.run();
    a.stop();
    cout << "***********************" << endl;
    RAM b(8, DDR3, 1600);
    b.run();
    b.stop();
    cout << "***********************" << endl;
    CD_ROM c(SATA, 2, built_in);
    c.run();
    c.stop();
    cout << "***********************" << endl;
    Computer my_computer(a, b, c, 128, 10);
    my_computer.run();
    my_computer.stop();
    return 0;
}

